package xyz.thoughtset.viewer.common.ai.model.factory;


import com.fasterxml.jackson.databind.ObjectMapper;
import lombok.Getter;
import lombok.NonNull;
import org.springframework.ai.chat.model.ChatModel;
import org.springframework.ai.model.Model;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.ObjectUtils;
import org.springframework.util.StringUtils;
import xyz.thoughtset.viewer.common.ai.model.entity.AiNode;
import xyz.thoughtset.viewer.common.ai.model.entity.ModelParam;
import xyz.thoughtset.viewer.common.ai.model.entity.purpose.ModelPurposeEnum;

import java.util.List;
import java.util.Optional;

@Getter
public abstract class ModelBuilder {
    protected String provider;
    protected List<String> supportModels;
//
//    protected ModelPurposeEnum purposeEnum;

    @Autowired
    protected ObjectMapper objectMapper;

    protected ModelBuilder(@NonNull String provider) {
        this(provider,"");
    }
    protected ModelBuilder(@NonNull String provider, String aiModel) {
        this(provider, List.of(aiModel));
    }

    protected ModelBuilder(String provider, List<String> supportModels) {
        this.provider = provider;
        this.supportModels = supportModels;
        ModelsRegistry.registerModel(this);
    }

    public boolean wasDefault(){return false;}

    public boolean checkExecModel(AiNode node, ModelParam modelParam){
        boolean checkFlag = ObjectUtils.isEmpty(this.supportModels);
        if (!checkFlag){
            for (String model : this.supportModels) {
                //todo: 模型模糊匹配
                if (model.equals(modelParam.getModel())){
                    checkFlag = true;
                    break;
                }
            }
        }
        return this.provider.equals(node.getProvider()) && checkFlag;
    }

    public abstract Model buildMode(AiNode node, ModelParam modelParam);

    public <T> void setOptionalValue(T value, java.util.function.Consumer<T> setter) {
        Optional.ofNullable(value).ifPresent(setter);
    }


}
